import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;


public class GreyIcon extends VisIcon {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8578434518403590848L;

	GreyIcon(String filename) {
		super(filename);
	}

	GreyIcon(Mass mass) {
		super();
		BufferedImage image = new BufferedImage(getIconWidth(), getIconHeight(), BufferedImage.TYPE_INT_RGB);
		// white background
		for(int x=0; x<getIconWidth(); x++) {
			for(int y=0; y<getIconHeight(); y++) {
				image.setRGB(x, y, Color.WHITE.getRGB());
			}
		}
		
		double standardDeviation = Math.sqrt(new Float(mass.Variance_Grey.value).doubleValue()); 
		for(int x=0; x<getIconWidth(); x++) {
			if(x < mass.Average_Grey.value - 2 * standardDeviation) {
				// draw stick
			}
			else if(x < mass.Average_Grey.value + 2 * standardDeviation) {
				// paint (fill) average grey
				// stolen transparency code from:
				// http://www.apl.jhu.edu/~hall/java/Java2D-Tutorial.html#Java2D-Tutorial-Transparency
				int value = (int) mass.Average_Grey.value;
				Graphics2D g2d = image.createGraphics();
				Composite originalComposite = g2d.getComposite();
				g2d.setPaint(new Color(value, value, value));
				g2d.drawLine(x, 25, x, 74);
				g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, mass.Average_Grey.certainty));
				g2d.drawLine(x, 0, x, 24);
				g2d.drawLine(x, 75, x, getIconHeight());
				g2d.setComposite(originalComposite);
			}
			else {
				// draw stick
			}
			if(x == (int) (mass.Average_Grey.value - 2 * standardDeviation)) {
				drawLineWithImportance(mass, "Variance_Grey", image, x);
			}
			else if(x == (int) (mass.Average_Grey.value)) {
				drawLineWithImportance(mass, "Average_Grey", image, x);
			}
			else if(x == (int) (mass.Average_Grey.value + 2 * standardDeviation)) {
				drawLineWithImportance(mass, "Variance_Grey", image, x);
			}
		}
		setImage(image);
	}

	private void drawLineWithImportance(Mass mass, String fieldName, BufferedImage image, int x) {
		try {
			float importance = ((Feature)Mass.class.getField(fieldName).get(mass)).importance;
			float certainty = ((Feature)Mass.class.getField(fieldName).get(mass)).certainty;
			Color color;
			if(importance < 0.3) {
				color = Color.GREEN;
			} else if (importance < 0.7) {
				color = Color.YELLOW;
			} else {
				color = Color.RED;
			}
			for(int y = 0; y < getIconHeight(); y++) {
				int result = Util.addAlpha((int)(certainty * 255), color.getRGB());
				image.setRGB(x - 1, y, result);
				image.setRGB(x, y, result);
				image.setRGB(x + 1, y, result);
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public int getIconHeight() {
		return 100;
	}

	@Override
	public int getIconWidth() {
		return 255;
	}
}
